//
//  LAMerchantDetailTableView.m
//  LYLazizDevelopDemo
//
//  Created by xbn_iMac on 2016/10/26.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LAMerchantDetailTableView.h"
#import "LASegmentView.h"
#import "LAMerchantDetailVideoTableView.h"
#import "LAMerchantDetailReviewTableView.h"
#import "LAMerchantPhotoTableView.h"
#import "LAMerchantInformationTableView.h"
#import "LAMerchantDetailHeaderView.h"
#import "LZRestaurantDetailModel.h"

@interface LAMerchantDetailTableView ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) LAMerchantDetailHeaderView *headerView;

@property (nonatomic, assign) BOOL canScroll;
@property (nonatomic, assign) BOOL isTopIsCanNotMoveTabView;
@property (nonatomic, assign) BOOL isTopIsCanNotMoveTabViewPre;
@property (nonatomic, strong) LASegmentView * LASegView;

@property (nonatomic, assign) CGFloat headerHeight;

@property (nonatomic, assign) NSInteger selectedButtonTag;  // 记录点击的按钮

@end

@implementation LAMerchantDetailTableView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.delegate=self;
    self.dataSource=self;
    self.decelerationRate = 0.8;
    self.showsVerticalScrollIndicator = NO;
    self.separatorColor = [UIColor clearColor];
    
    /*
     *
     */
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acceptMsg:) name:@"leaveTop" object:nil];
    
    // Initialization code
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setModel:(LZRestaurantDetailModel *)model {
    _model = model;
    [self createHeaderView];
    self.informationTable.model = model;
}

// 页面展示的处理方法
- (void)viewShow {
    if (self.selectedButtonTag == 0) {
        [self.videoTable startPlayVideo];
    }
}

// 页面消失的处理方法
- (void)viewDiss {
    if (self.selectedButtonTag == 0) {
        [self.videoTable stopPlayVideo];
    }
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

-(void)acceptMsg : (NSNotification *)notification{
    
    NSDictionary *userInfo = notification.userInfo;
    NSString *canScroll = userInfo[@"canScroll"];
    if ([canScroll isEqualToString:@"1"]) {
        _canScroll = YES;
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    /**
     * 处理联动
     */

    if (self.detailDelegate && [self.detailDelegate respondsToSelector:@selector(detailViewDidScroll:)]) {
        [self.detailDelegate detailViewDidScroll:self];
    }
    
    //获取滚动视图y值的偏移量    
    CGFloat tabOffsetY = [self rectForSection:0].origin.y-64+10;
    CGFloat offsetY = scrollView.contentOffset.y+1;
    
    _isTopIsCanNotMoveTabViewPre = _isTopIsCanNotMoveTabView;
    if (offsetY>=tabOffsetY) {
        scrollView.contentOffset = CGPointMake(0, tabOffsetY);
        _isTopIsCanNotMoveTabView = YES;
    }else{
        _isTopIsCanNotMoveTabView = NO;
    }
    if (_isTopIsCanNotMoveTabView != _isTopIsCanNotMoveTabViewPre) {
        if (!_isTopIsCanNotMoveTabViewPre && _isTopIsCanNotMoveTabView) {
//            NSLog(@"滑动到顶端");
            [[NSNotificationCenter defaultCenter] postNotificationName:@"goTop" object:nil userInfo:@{@"canScroll":@"1"}];
            _canScroll = NO;
            if (self.detailDelegate && [self.detailDelegate respondsToSelector:@selector(detailViewCanNotScroll)]) {
                [self.detailDelegate detailViewCanNotScroll];
            }

        }
        if(_isTopIsCanNotMoveTabViewPre && !_isTopIsCanNotMoveTabView){
//            NSLog(@"离开顶端");
            if (!_canScroll) {
                scrollView.contentOffset = CGPointMake(0, tabOffsetY);
            }
        }
    }
}

#pragma marl -tableDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return SCREEN_HEIGHT-64;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //添加pageView
    [cell.contentView addSubview:self.setPageViewControllers];
    
    return cell;
}

/*
 * 这里可以任意替换你喜欢的pageView
 */
-(UIView *)setPageViewControllers
{
    if (!_LASegView) {
        
        NSArray *controllers=@[self.videoTable,self.reviewTable,self.photoTable,self.informationTable];
        
        NSArray *titleArray =@[LZLocalizedString(@"restaurant_video", nil),LZLocalizedString(@"restaurant_review", nil),LZLocalizedString(@"restaurant_photo_album", nil),LZLocalizedString(@"restaurant_information", nil)];
        
        LASegmentView * rcs=[[LASegmentView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) controllers:controllers titleArray:titleArray];
        
        _LASegView = rcs;
        
        WS(weakSelf)
        rcs.selectedButtonTag = ^(NSInteger tag) {
            weakSelf.selectedButtonTag = tag;
            if (tag == 0) {
                [weakSelf.videoTable startPlayVideo];
            }
            else {
                [weakSelf.videoTable stopPlayVideo];
            }
            [weakSelf checkTableViewHaveDataArray:tag];
        };
    }
    return _LASegView;
}

- (void)checkTableViewHaveDataArray:(NSInteger)tag {
    
    switch (tag) {
        case 0:
        {
            [self.videoTable checkHaveData];
        }
            break;
        case 1:
        {
            [self.reviewTable checkHaveData];
        }
            break;
        case 2:
        {
            // 餐厅相册第一版先去掉
            [self.photoTable checkHaveData];
        }
            break;
        case 3:
        {
            [self.informationTable checkHaveData];
        }
            break;
            
        default:
            break;
    }
}

- (LAMerchantDetailVideoTableView*)videoTable {
    if (_videoTable) {
        return _videoTable;
    }
    LAMerchantDetailVideoTableView * table=[[LAMerchantDetailVideoTableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _videoTable = table;
    return _videoTable;
}

- (LAMerchantDetailReviewTableView *)reviewTable {
    if (_reviewTable) {
        return _reviewTable;
    }
    LAMerchantDetailReviewTableView *table = [[LAMerchantDetailReviewTableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    table.reviewType = ReviewType_Restaurants;
    _reviewTable = table;
    return _reviewTable;
}

- (LAMerchantPhotoTableView*)photoTable {
    if (_photoTable) {
        return _photoTable;
    }
    LAMerchantPhotoTableView *table = [[LAMerchantPhotoTableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _photoTable = table;
    return _photoTable;
}

- (LAMerchantInformationTableView*)informationTable {
    if (_informationTable) {
        return _informationTable;
    }
    LAMerchantInformationTableView *table = [[LAMerchantInformationTableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _informationTable = table;
    return _informationTable;
}

-(void)createHeaderView {

    self.headerView = [LAMerchantDetailHeaderView instanceLAMerchantDetailHeaderView];
    self.headerView.haveADView = YES;
    self.headerView.model = self.model;
    
    self.headerHeight = [self.headerView judgeHeaderViewHeight];

    UIView *headerTemp = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.headerHeight)];
    self.tableHeaderView = headerTemp;

    self.headerView.frame = headerTemp.bounds;
    [headerTemp addSubview:self.headerView];
}

@end
